﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using ISS.IR.RRNMS.Library.BusinessLogic;


namespace ISS.IR.Web
{
    public partial class AccountPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitialLookups lookups = RRNMS.Library.BusinessLogic.Account.GetInitialLookupsForAccounts();

                // Gender
                rdoGender.DataSource = lookups.Genders;
                rdoGender.DataBind();

                ddlSubAccountGender.DataSource = lookups.Genders;
                ddlSubAccountGender.DataBind();

                // Governorate
                ddlGovernorate.DataSource = lookups.Governorates;
                ddlGovernorate.DataBind();

                //Region
                ddlDistrict.DataSource = lookups.Regions;
                ddlDistrict.DataBind();

                //Marital Status
                ddlMaritalStatus.DataSource = lookups.MaritalStatuses;
                ddlMaritalStatus.DataBind();

                ddlSubAccountMaritalStatus.DataSource = lookups.MaritalStatuses;
                ddlSubAccountMaritalStatus.DataBind();

                //Living Status
                rdoLivingStatus.DataSource = lookups.LivingStatuses;
                rdoLivingStatus.DataBind();

                //Chronic Disease
                ddlChronicDisease.DataSource = lookups.ChronicDiseases;
                ddlChronicDisease.DataBind();

                // Sub Account Relationship
                ddlSubAccountRelationship.DataSource = lookups.SubAccountRelationship;
                ddlSubAccountRelationship.DataBind();

                // Aid Types
                //ddlAidType.DataSource = lookups.AidTypes;
                //ddlAidType.DataBind();

                //GridView1.DataSource = dt;
                //GridView1.DataBind();

                ddlQuickSearchAccount.DataSource = ISS.IR.RRNMS.Library.BusinessLogic.Account.GetAllAccounts();
                ddlQuickSearchAccount.DataBind();

                if (Request.QueryString.AllKeys.Contains("id"))
                {
                    Guid AccountId = Guid.Parse(Request["id"]);

                    ISS.IR.RRNMS.Library.BusinessLogic.Account x = new ISS.IR.RRNMS.Library.BusinessLogic.Account(AccountId);

                    Load(x);
                }
            }

        }

        private void Save()
        {
            ISS.IR.RRNMS.Library.BusinessLogic.Account account = new ISS.IR.RRNMS.Library.BusinessLogic.Account();

            account.Action10Description = txtAction10Description.Text;
            account.Action11Description = txtAction11Description.Text;
            account.Action1Description = txtAction1Description.Text;
            account.Action2Description = txtAction2Description.Text;
            account.Action3Description = txtAction3Description.Text;
            account.Action4Description = txtAction4Description.Text;
            account.Action5Description = txtAction5Description.Text;
            account.Action6Description = txtAction6Description.Text;
            account.Action7Description = txtAction7Description.Text;
            account.Action8Description = txtAction8Description.Text;
            account.Action9Description = txtAction9Description.Text;

            account.AddressInEgypt = txtAddressInEgypt.Text;

            //account.AidAmount = String.IsNullOrEmpty(txtAidAmount.Text) ? 0 : decimal.Parse(txtAidAmount.Text);
            //account.AidCode = txtAidCode.Text;
            account.AidMonths = String.IsNullOrEmpty(txtAidMonths.Text) ? 0 : Int32.Parse(txtAidMonths.Text);
            //account.AidOther = txtAidOther.Text;
            //account.AidType = ddlAidType.SelectedIndex < 0 ? new RRNMS.Library.Item() : new RRNMS.Library.Item() { Id = Int32.Parse(ddlAidType.SelectedValue) };
            account.AverageMonthlyIncome = String.IsNullOrEmpty(txtAverageMonthlyIncome.Text) ? 0 : decimal.Parse(txtAverageMonthlyIncome.Text);
            account.BirthDate = String.IsNullOrEmpty(txtBirthDate.Text) ? new DateTime() : Convert.ToDateTime(txtBirthDate.Text);
            account.BirthPlace = txtBirthPlace.Text;

            account.ChronicDisease = ddlChronicDisease.SelectedIndex < 0 ? new RRNMS.Library.Item() : new RRNMS.Library.Item() { Id = Int32.Parse(ddlChronicDisease.SelectedValue) };
            account.ChronicDiseaseOther = txtChronicDiseaseOther.Text;
            account.CommitteeDecision = txtCommitteeDecision.Text;

            account.DetentionInEgyptDescription = txtDetentionInEgyptDescription.Text;
            account.DetentionInHomeCountryDescription = txtDetentionInHomeCountryDescription.Text;

            account.EducationAmount = String.IsNullOrEmpty(txtEducationAmount.Text) ? 0 : decimal.Parse(txtEducationAmount.Text);
            account.ElectricityAmount = String.IsNullOrEmpty(txtElectricityAmount.Text) ? 0 : decimal.Parse(txtElectricityAmount.Text);

            account.Email = txtEmail.Text;

            account.EmergencyDescription = txtEmergencyDescription.Text;

            account.FamilyMembersCount = String.IsNullOrEmpty(txtFamilyMembersCount.Text) ? 0 : Int32.Parse(txtFamilyMembersCount.Text);

            account.FirstName = txtFirstName.Text;
            account.FoodAmount = String.IsNullOrEmpty(txtFoodAmount.Text) ? 0 : decimal.Parse(txtFoodAmount.Text);
            account.FourthName = txtFourthName.Text;

            account.GasAmount = String.IsNullOrEmpty(txtGasAmount.Text) ? 0 : decimal.Parse(txtGasAmount.Text);
            account.Gender = rdoGender.SelectedIndex != -1 ? new RRNMS.Library.Item() { Id = Int32.Parse(rdoGender.SelectedValue) } : new RRNMS.Library.Item();
            account.HasDisability = rdoHasDisabilityYes.Checked;
            account.HasDrinkingWater = rdoHasDrinkingWaterYes.Checked;
            account.HasElectricity = rdoHasElectricityYes.Checked;
            account.HasFurniture = rdoHasFurnitureYes.Checked;

            account.HasPregnancyProblems = rdoHasPregnancyProblemsYes.Checked;
            account.HasPsychiatricDisease = rdoHasPsychiatricDiseaseYes.Checked;
            account.HasSanitation = rdoHasSanitationYes.Checked;

            account.InfantsCount = String.IsNullOrEmpty(txtInfantsCount.Text) ? 0 : Int32.Parse(txtInfantsCount.Text);
            account.InterviewSummary = txtInterviewSummary.Text;

            account.IsAction1 = chkAction1.Checked;
            account.IsAction2 = chkAction2.Checked;
            account.IsAction3 = chkAction3.Checked;
            account.IsAction4 = chkAction4.Checked;
            account.IsAction5 = chkAction5.Checked;
            account.IsAction6 = chkAction6.Checked;
            account.IsAction7 = chkAction7.Checked;
            account.IsAction8 = chkAction8.Checked;
            account.IsAction9 = chkAction9.Checked;
            account.IsAction10 = chkAction10.Checked;
            account.IsAction11 = chkAction11.Checked;

            account.IsDisabilityMedicalReportAttached = chkDisabilityMedicalReportAttached.Checked;
            account.IsInEmergency = chkIsInEmergency.Checked;
            account.IsPregnancyProblemsMedicalReportAttached = chkPregnancyProblemsMedicalReportAttached.Checked;
            account.IsPregnant = rdoIsPregnantYes.Checked;
            account.IsPregnantMedicalReportAttached = chkPregnantMedicalReportAttached.Checked;
            account.IsPsychiatricDiseaseMedicalReportAttached = chkPsychiatricDiseaseMedicalReportAttached.Checked;

            account.JobDescriptionInHomeCountry = txtJobDescriptionInHomeCountry.Text;
            account.JobWantedInEgypt = txtJobWantedInEgypt.Text;

            account.LeaseAmount = String.IsNullOrEmpty(txtLeaseAmount.Text) ? 0 : decimal.Parse(txtLeaseAmount.Text);
            account.LivingStatus = rdoLivingStatus.SelectedIndex > -1 ? new RRNMS.Library.Item() { Id = Int32.Parse(rdoLivingStatus.SelectedValue) } : new RRNMS.Library.Item();

            account.MaritalStatus = ddlMaritalStatus.SelectedIndex > 0 ? new RRNMS.Library.Item() { Id = Int32.Parse(ddlMaritalStatus.SelectedValue) } : new RRNMS.Library.Item();

            account.MedicineAmount = String.IsNullOrEmpty(txtMedicineAmount.Text) ? 0 : decimal.Parse(txtMedicineAmount.Text);
            account.NumberOfRooms = String.IsNullOrEmpty(txtNumberOfRooms.Text) ? 0 : Int32.Parse(txtNumberOfRooms.Text);
            account.OtherAmount = String.IsNullOrEmpty(txtOtherAmount.Text) ? 0 : decimal.Parse(txtOtherAmount.Text);

            account.OtherIncomeSource = txtOtherIncomeSource.Text;

            account.SecondName = txtSecondName.Text;
            account.SocialWorkerDecisionCode = txtSocialWorkerDecisionCode.Text;
            account.SocialWorkerDecisionDescription = txtSocialWorkerDecisionDescription.Text;
            account.Telephone = txtTelephone.Text;
            account.ThirdName = txtThirdName.Text;

            account.TrainingCoursesWantedInEgypt = txtTrainingCoursesWantedInEgypt.Text;

            account.UNHCR = txtUNHCR.Text;

            account.WasDetainedInEgypt = chkWasDetainedInEgypt.Checked;
            account.WasDetainedInHomeCountry = chkWasDetainedInHomeCountry.Checked;

            account.WaterAmount = String.IsNullOrEmpty(txtWaterAmount.Text) ? 0 : decimal.Parse(txtWaterAmount.Text);

            
            
            account.Save();
        }

        private void Load(Account account)
        {

            txtAction10Description.Text = account.Action10Description;
            txtAction11Description.Text = account.Action11Description;
            txtAction1Description.Text = account.Action1Description;
            txtAction2Description.Text = account.Action2Description;
            txtAction3Description.Text = account.Action3Description;
            txtAction4Description.Text = account.Action4Description;
            txtAction5Description.Text = account.Action5Description;
            txtAction6Description.Text = account.Action6Description;
            txtAction7Description.Text = account.Action7Description;
            txtAction8Description.Text = account.Action8Description;
            txtAction9Description.Text = account.Action9Description;

            txtAddressInEgypt.Text = account.AddressInEgypt;

            //txtAidAmount.Text = account.AidAmount.ToString();
            //txtAidCode.Text = account.AidCode;
            //txtAidMonths.Text = account.AidMonths.ToString();
            //txtAidOther.Text = account.AidOther;

            //if (account.AidType.Id != 0)
            //{
            //    ddlAidType.SelectedItem.Selected = false;
            //    ddlAidType.Items.FindByValue(account.AidType.ToString()).Selected = true;
            //}

            txtAverageMonthlyIncome.Text = account.AverageMonthlyIncome.ToString();
            if (account.BirthDate != new DateTime())
                txtBirthDate.Text = account.BirthDate.Date.ToString();
            txtBirthPlace.Text = account.BirthPlace;

            if (account.ChronicDisease.Id != 0)
            {
                ddlChronicDisease.SelectedItem.Selected = false;
                ddlChronicDisease.Items.FindByValue(account.ChronicDisease.Id.ToString()).Selected = true;
            }

            txtChronicDiseaseOther.Text = account.ChronicDiseaseOther;
            txtCommitteeDecision.Text = account.CommitteeDecision;

            txtDetentionInEgyptDescription.Text = account.DetentionInEgyptDescription;
            txtDetentionInHomeCountryDescription.Text = account.DetentionInHomeCountryDescription;

            txtEducationAmount.Text = account.EducationAmount.ToString();
            txtElectricityAmount.Text = account.ElectricityAmount.ToString();

            txtEmail.Text = account.Email;

            txtEmergencyDescription.Text = account.EmergencyDescription;

            txtFamilyMembersCount.Text = account.FamilyMembersCount.ToString();

            txtFirstName.Text = account.FirstName;
            txtFoodAmount.Text = account.FoodAmount.ToString();
            txtFourthName.Text = account.FourthName;

            txtGasAmount.Text = account.GasAmount.ToString();

            if (account.Gender.Id != 0)
            {
                if (rdoGender.SelectedItem != null)
                    rdoGender.SelectedItem.Selected = false;
                rdoGender.Items.FindByValue(account.Gender.Id.ToString()).Selected = true;
            }

            rdoHasDisabilityYes.Checked = account.HasDisability;
            rdoHasDrinkingWaterYes.Checked = account.HasDrinkingWater;
            rdoHasElectricityYes.Checked = account.HasElectricity;
            rdoHasFurnitureYes.Checked = account.HasFurniture;

            rdoHasPregnancyProblemsYes.Checked = account.HasPregnancyProblems;
            rdoHasPsychiatricDiseaseYes.Checked = account.HasPsychiatricDisease;
            rdoHasSanitationYes.Checked = account.HasSanitation;

            txtInfantsCount.Text = account.InfantsCount.ToString();
            txtInterviewSummary.Text = account.InterviewSummary;

            chkAction1.Checked = account.IsAction1;
            chkAction2.Checked = account.IsAction2;
            chkAction3.Checked = account.IsAction3;
            chkAction4.Checked = account.IsAction4;
            chkAction5.Checked = account.IsAction5;
            chkAction6.Checked = account.IsAction6;
            chkAction7.Checked = account.IsAction7;
            chkAction8.Checked = account.IsAction8;
            chkAction9.Checked = account.IsAction9;
            chkAction10.Checked = account.IsAction10;
            chkAction11.Checked = account.IsAction11;

            chkDisabilityMedicalReportAttached.Checked = account.IsDisabilityMedicalReportAttached;
            chkIsInEmergency.Checked = account.IsInEmergency;
            chkPregnancyProblemsMedicalReportAttached.Checked = account.IsPregnancyProblemsMedicalReportAttached;
            rdoIsPregnantYes.Checked = account.IsPregnant;
            chkPregnantMedicalReportAttached.Checked = account.IsPregnantMedicalReportAttached;
            chkPsychiatricDiseaseMedicalReportAttached.Checked = account.IsPsychiatricDiseaseMedicalReportAttached;

            txtJobDescriptionInHomeCountry.Text = account.JobDescriptionInHomeCountry;
            txtJobWantedInEgypt.Text = account.JobWantedInEgypt;

            txtLeaseAmount.Text = account.LeaseAmount.ToString();
            if (account.LivingStatus.Id != 0)
            {
                if (rdoLivingStatus.SelectedItem != null)
                    rdoLivingStatus.SelectedItem.Selected = false;
                rdoLivingStatus.Items.FindByValue(account.LivingStatus.Id.ToString());
            }

            if (account.MaritalStatus.Id != 0)
            {
                if (ddlMaritalStatus.SelectedItem != null)
                    ddlMaritalStatus.SelectedItem.Selected = false;
                if (ddlMaritalStatus.Items.FindByValue(account.MaritalStatus.Id.ToString()) != null)
                    ddlMaritalStatus.Items.FindByValue(account.MaritalStatus.Id.ToString()).Selected = true;
            }

            txtMedicineAmount.Text = account.MedicineAmount.ToString();
            txtNumberOfRooms.Text = account.NumberOfRooms.ToString();
            txtOtherAmount.Text = account.OtherAmount.ToString();

            txtOtherIncomeSource.Text = account.OtherIncomeSource;

            txtSecondName.Text = account.SecondName;
            txtSocialWorkerDecisionCode.Text = account.SocialWorkerDecisionCode;
            txtSocialWorkerDecisionDescription.Text = account.SocialWorkerDecisionDescription;
            txtTelephone.Text = account.Telephone;
            txtThirdName.Text = account.ThirdName;

            txtTrainingCoursesWantedInEgypt.Text = account.TrainingCoursesWantedInEgypt;

            txtUNHCR.Text = account.UNHCR;

            chkWasDetainedInEgypt.Checked = account.WasDetainedInEgypt;
            chkWasDetainedInHomeCountry.Checked = account.WasDetainedInHomeCountry;

            txtWaterAmount.Text = account.WaterAmount.ToString();

        }

        protected void btnLoadAccount_Click(object sender, EventArgs e)
        {
            Account acc = new Account();
            if (rdoQuickSearchUNHCR.Checked)
                acc = new Account(txtQuickSearchUNHCR.Text);
            else
                acc = new Account(Guid.Parse(ddlQuickSearchAccount.SelectedValue));

            Load(acc);
        }

        protected void btnSaveAccount_Click(object sender, EventArgs e)
        {
            Save();
        }

        protected void btnSaveSubAccount_Click(object sender, EventArgs e)
        {
            SaveSubAccount();
        }

        private void SaveSubAccount()
        {
            ISS.IR.RRNMS.Library.BusinessLogic.SubAccount subAccount = new ISS.IR.RRNMS.Library.BusinessLogic.SubAccount();

            subAccount.FirstName = txtSubAccountFirstName.Text;
            subAccount.SecondName = txtSubAccountSecondName.Text;
            subAccount.ThirdName = txtSubAccountThirdName.Text;
            subAccount.FourthName = txtSubAccountFourthName.Text;

            subAccount.HasYellowCard = chkSubAccountHasYellowCard.Checked;

            subAccount.Gender = ddlSubAccountGender.SelectedIndex != -1 ? new RRNMS.Library.Item() { Id = Int32.Parse(ddlSubAccountGender.SelectedValue), Value = ddlSubAccountGender.SelectedItem.Text } : new RRNMS.Library.Item();
            subAccount.SubAccountRelationship = ddlSubAccountRelationship.SelectedIndex != -1 ? new RRNMS.Library.Item() { Id = Int32.Parse(ddlSubAccountRelationship.SelectedValue), Value = ddlSubAccountRelationship.SelectedItem.Text } : new RRNMS.Library.Item();

            subAccount.MaritalStatus = ddlSubAccountMaritalStatus.SelectedIndex != -1 ? new RRNMS.Library.Item() { Id = Int32.Parse(ddlSubAccountMaritalStatus.SelectedValue), Value = ddlSubAccountMaritalStatus.SelectedItem.Text } : new RRNMS.Library.Item();

            subAccount.BirthDate = String.IsNullOrEmpty(txtSubAccountBirthDate.Text) ? new DateTime() : Convert.ToDateTime(txtSubAccountBirthDate.Text);
            List<SubAccount> subAccounts = new List<SubAccount>();

            if (Session["SubAccounts"] != null)
            {
                subAccounts = (List<SubAccount>)Session["SubAccounts"];


                if (Session["SubAccountId"] != null)
                {
                    SubAccount foundSubAccount = subAccounts.Find(delegate(SubAccount s) { return s.SubAccountId == (Guid)Session["SubAccountId"]; });


                }
                else
                {
                    subAccounts.Add(subAccount);

                }

            }
            else
            {
                subAccounts.Add(subAccount);
                Session["SubAccounts"] = subAccounts;
            }

            if (subAccounts != null && subAccounts.Count != 0)
            {
                grdSubAccounts.DataSource = subAccounts;
                grdSubAccounts.DataBind();
            }
        }
    }
}